// Copyright@ChenChao


#include "Items/Item.h"

#include "Components/SphereComponent.h"
// #include “Components/PrimitiveComponent.h”
#include "Characters/RPG_CharacterBase.h"
#include "RPG/DebugMacros.h"


// 构造函数
AItem::AItem()
{
	// 将此参与者设置为每帧调用Tick（）。如果你不需要它，你可以关闭它来提高性能。
	PrimaryActorTick.bCanEverTick = true;

	ItemMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ItemMeshComponent"));
	RootComponent = ItemMesh;

	/*球体组件*/
	SphereComp = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComponent"));
	SphereComp->SetupAttachment(GetRootComponent());

}

// 在游戏开始或生成时调用
void AItem::BeginPlay()
{
	Super::BeginPlay();

	/*球体重叠委托*/
	SphereComp->OnComponentBeginOverlap.AddDynamic(this, &AItem::F_OnSphereOverlap);
	SphereComp->OnComponentEndOverlap.AddDynamic(this, &AItem::F_OnSphereEndOverlap);
}

float AItem::F_Sin() const
{
	return Amplitude * FMath::Sin(RunningTime * TimeConstant);
}

float AItem::F_Cos() const
{
	return Amplitude * FMath::Cos(RunningTime * TimeConstant);
}

// Called every frame
void AItem::Tick(const float DeltaTime)
{
	Super::Tick(DeltaTime);
	RunningTime += DeltaTime;

	/*
	//绘制调试球体
	const FVector StartPosition = GetActorLocation();
	const FVector ActorForwardVector = GetActorForwardVector();
	const FVector EndPosition = StartPosition + ActorForwardVector * 100.f;
	DRAW_DEBUG_VECTOR_SigleFrame(StartPosition, EndPosition);
	*/

	//设置物体浮动
	F_Float();
	
}


/*****************************************************************************************************************/
/*重叠委托回调函数*/
void AItem::F_OnSphereOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
	UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	ARPG_CharacterBase* CharacterBase = CastChecked<ARPG_CharacterBase>(OtherActor);
	if (CharacterBase)
	{
		CharacterBase->SetOverlappingItem(this);
	}
}

void AItem::F_OnSphereEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
	UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	ARPG_CharacterBase* CharacterBase = CastChecked<ARPG_CharacterBase>(OtherActor);
	if (CharacterBase)
	{
		CharacterBase->SetOverlappingItem(nullptr);
	}
}
/*****************************************************************************************************************/
/*物体上下浮动函数*/
void AItem::F_Float()
{
	if (ItemState == EItemState::EIS_Hovering)
	{
		AddActorWorldOffset(FVector(0.f, 0.f, F_Sin()));
	}
}


